# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=freecad
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.0
pkgrel=4
_OS_commit=91f70382
pkgdesc="Free and Opensource multiplatform 3D parametric modeler (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://freecadweb.org/'
msys2_repository_url="https://github.com/FreeCAD/FreeCAD"
msys2_references=(
  'archlinux: freecad'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-coin"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-med"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-opencascade"
         "${MINGW_PACKAGE_PREFIX}-openscad"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-pivy"
         "${MINGW_PACKAGE_PREFIX}-pyside6"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-xerces-c"
         "${MINGW_PACKAGE_PREFIX}-vtk"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-python-matplotlib"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-microsoft-gsl"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
source=("https://github.com/FreeCAD/FreeCAD/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/Ondsel-Development/OndselSolver/archive/${_OS_commit}/OndselSolver-${_OS_commit}.tar.gz"
        "001-clang19-char-traits.patch"
        "002-vtk-9.4.patch::https://github.com/FreeCAD/FreeCAD/pull/18543.patch")
sha256sums=('51115afd8e3b89f4bac343b986b0800d8e1c68fc1e322c3cd179cfe1033ef937'
            '061f651f503f1ec5a9814e174cdb8a9ba84d511a3b8be3d94852e84976c6b158'
            '0dcc9a5ce20f080913f2bff71f5e2eb7d166e789f1012cea2009f94c9f30e5f2'
            'aaf53617dff2119805b2f4d415181d7685e43b80e18905d0f64e2b909afc8510')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  mv OndselSolver-*/* ${_realname}-${pkgver}/src/3rdParty/OndselSolver/

  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    001-clang19-char-traits.patch \
    002-vtk-9.4.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CC} == clang ]]; then
    CXXFLAGS+=" -Wno-macro-redefined"
  else
    CXXFLAGS+=" -Wno-template-id-cdtor"
  fi

  CXXFLAGS+=" -Wno-deprecated-declarations -Wno-unknown-pragmas" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DFREECAD_QT_VERSION="6" \
      -DBUILD_TEST=OFF \
      -DBUILD_WEB=OFF \
      -DBUILD_ADDONMGR=OFF \
      -DENABLE_DEVELOPER_TESTS=OFF \
      -DINSTALL_TO_SITEPACKAGES=OFF \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      "${_extra_config[@]}" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  MSYS2_ARG_CONV_EXCL="-d" \
    python -m compileall -o 0 -o 1 -q -d"${MINGW_PREFIX}" "${pkgdir}${MINGW_PREFIX}"
}
